package com.steakliu.sun.client.netty.support;

import com.steakliu.sun.client.api.ClientConfig;
import com.steakliu.sun.registry.api.RegistryConfig;
import com.steakliu.sun.registry.api.RegistryService;
import com.steakliu.sun.spi.loader.ExtensionLoader;

import java.util.HashMap;
import java.util.Map;

/**
 * 功能说明：
 * <p>
 * Original @Author: steakliu-刘牌, 2023-01-08  22:27
 * <p>
 * Copyright (C)2020-2022  steakliu All rights reserved.
 */
public class RegistryServiceSupport {

    private static final Map<String,RegistryService> registryServiceCache = new HashMap<>();

    /**
     * 获取注册中心服务
     * @param config
     * @return
     * @throws Exception
     */
//    public static RegistryService getRegistryServiceInstance(ClientConfig config) throws Exception {
//        String key = buildKey(config.getRegistryType(), config.getRegistryAddress(), config.getLoadBalance());
//        RegistryService registryService = registryServiceCache.get(key);
//        if (null == registryService){
//            registryService = ExtensionLoader.getExtension(RegistryService.class, config.getRegistryType())
//                    .init(new RegistryConfig(config.getLoadBalance(), config.getRegistryAddress()));
//            if (null != registryService){
//                registryServiceCache.put(key, registryService);
//            }
//        }
//        return registryService;
//    }


    /**
     * 构建注册中心唯一标识
     * @param registryType
     * @param registryAddress
     * @param loadBalanceType
     * @return
     */
    private static String buildKey(String registryType, String registryAddress, String loadBalanceType) {
        return String.join("#", registryType, registryAddress, loadBalanceType);
    }
}
